import useAiStore from '../../../store/ai';
const store = useAiStore();
import common from './common.js'

let _dsbridge
///仅在h5模式下加载_dsbridge，再小程序模式下会报错
// #ifdef H5
_dsbridge = require('dsbridge')
// #endif

let runOnPhone
// #ifdef H5
runOnPhone = _dsbridge.hasNativeMethod('deviceInfoAsyn')
// #endif
// #ifndef H5
runOnPhone = false
// #endif

const dsbridge = {
	/* 注册一个JS方法，供原生调用 */
	registermethod(tag, callback) {
		_dsbridge.register(tag, callback)
	},
	///调用原生方法的封装
	callmethod(name, data, callback) {
		if (!runOnPhone) {
			console.log('当前不运行在手机上，与原生交互失败！！！！ error error')
			return
		}
		if (!_dsbridge.hasNativeMethod(name)) {
			console.log('调用了一个不存在的原生方法，方法名是：', name,)
		}
		return _dsbridge.call(name, data, callback)
	},
	/*判断时候运行在有dsBridgr和交互的环境 */
	isRunOnPhone: runOnPhone,

	/* 获取手机设备信息，包含导航栏高度等 */
	getSystemInfo(callback) {
		this.callmethod('deviceInfoAsyn', '', callback);
	},
	/* 是否已经是登录状态 */
	isLogin() {
		return this.callmethod('isLoginSyn')
	},
	/* 是否正在执行登录流程 */
	isLogging() {
		return this.callmethod('isLoggingSyn')
	},
	/* 跳转调用原生的跳转 */
	showLoginPage(callback) {
		this.callmethod('nativeLoginAsyn', '', callback)
	},
	/* 在需要登录的时候调用，在判断是否已经在登录界面之后执行登录，并在登陆之后回调 */
	needLogin(callback) {
		if (callback == undefined) {
			callback = () => { }
		}
		if (this.isLogging()) {
			if (callback) {
				callback(false)
			}
		} else {
			this.showLoginPage(callback)
		}
	},
	/* 主动获取token */
	getNativeToken() {
		const token = this.callmethod('getTokenSyn')
		uni.setStorage({
			key: 'Authentication',
			data: token
		})
		return token
	},
	/* 设置是否隐藏导航栏 */
	setTabBarIsHiden(value) {
		this.callmethod('setTabBarIsHiden', { isHidden: value })
	},
	/* 调用原生的返回 */
	navigateBack() {
		this.callmethod('navigateBack')
	},
	makePhoneCall(phone) {
		this.callmethod('makePhoneCall', phone)
	},
	goHomePage() {
		const data = common.returnState().spmData
		this.sendSpmParamToNative({ ref: data.spm_cnt, pre: data.spm_ref })
		this.callmethod('goHomePage')
	},
	pay(value, callback) {
		this.callmethod('nativePayAsyn', value, callback)
	},
	makeVlog(value) {
		this.callmethod('makeVlog', value)
	},
	savedPhotosAlbum(value) {
		this.callmethod('savedPhotosAlbum', value)
	},
	openLocation(value) {
		this.callmethod('openLocation', value)
	},
	sendSpmParamToNative(value) {
		this.callmethod('sendSpmParamToNative', value)
	},
	// 获取原生埋点参数配置
	getAppSpmParam() {
		return this.callmethod('getAppSpmParam')
	},
	// 获取原生目的地
	getAppDest() {
		try {
			var dest = this.callmethod('getAppDestId')
			uni.setStorageSync("destItem", JSON.parse(dest))
		} catch (error) {

		}
	},
	/**
	 *
	 * @param { Object } dspIdObj  {dspId: "123456"} OR  {orderId: "987654"}
	 * @returns
	 */
	openDsp(dspIdObj = {}) {
		if (Object.keys(dspIdObj).length === 0) return

		const data = common.returnState().spmData
		this.sendSpmParamToNative({ ref: data.spm_cnt, pre: data.spm_ref })

		this.callmethod('openDsp', dspIdObj)
	},
	editJournelSure() {
		this.callmethod('editJournelSure')
	},
	submitOrderDone(parms) {
		this.callmethod('submitOrderDone', parms)
	},
	closeWebPopup() {
		this.callmethod('closeWebPopup')
	},
	openChat(params) {
		this.callmethod('openChat', params)
	},
	openEditedDSPList() {
		this.callmethod('openEditedDSPList')
	},
	/* 注册所有的web供原生调用的方法 */
	registerAllMethods() {
		this.registerUpdatehToken()
		this.registerNavigateBack()
		this.willCloseWebView()
	},
	/* 注册更新token回调，每当token变化原生将会回调此方法通知web更新token */
	registerUpdatehToken() {
		this.registermethod('updateToken', (token) => {
			uni.setStorageSync('Authentication', token);
			store.SET_TOKEN(token)
		})
	},
	/* 容器关闭原生将会回调此方法通知web */
	willCloseWebView() {
		this.registermethod('willCloseWebView', (a) => {
			common.spmFun(common.returnState().pointData, common.returnState().spmData)
		})
	},
	/* 注册返回事件，供原生调用（安卓物理返回） */
	registerNavigateBack() {
		this.registermethod('navigateBack', () => {
			let routes = getCurrentPages()
			if (routes.length <= 1) {
				///当物理返回建触发时，web路由深度已经是根路由时，调用原生的返回
				this.navigateBack()
			} else {
				///web可以返回时，web自己返回
				uni.navigateBack()
			}
		})
	},
	//app分享
	appShare(value) {
		this.callmethod('openNativeShare', value)
	},
	///容器是否提供分享能力
	canShare() {
		// #ifdef H5
		return _dsbridge.hasNativeMethod('openNativeShare')
		// #endif
		// #ifndef H5
		return false
		// #endif
	},
	//app关闭弹框
	navigateDismissBack() {
		this.callmethod('navigateDismissBack')
	},
	openNativeAICamera(value){
		this.callmethod('openNativeAICamera',value)
	},
	// 获取原生目的地
	getStorgePhoto() {
		try {
			var dest = this.callmethod('getStorgePhoto')
			console.log('dest',dest)
			// uni.setStorageSync("photo", JSON.parse(dest))
			return dest
		} catch (error) {

		}
	},
	savedPhotosAlbum(value){
		this.callmethod('savedPhotosAlbum',value)
	},
	savedNativeVideo(value){
		this.callmethod('savedVideo',value)
	},
	// 打开迪士尼小程序
	openDsnMiniProgram() {
		this.callmethod('openDsnMiniProgram')
	},
}
export default dsbridge
// module.exports = dsbridge